/**   
 * @Title: SettingsActivity.java 
 * @Package: com.rename.icode.activity.settings 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-3-5 上午9:28:02 
 * @version  
 */

package com.rename.icode.activity.settings;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.rename.icode.R;
import com.rename.icode.activity.ActivitySupport;
import com.rename.icode.manager.AppConfigManager;
import com.rename.icode.utils.LogUtil;

/**
 * @Description 设置界面
 * @author Administrator
 * @date 2015-3-5 上午9:28:02
 * @version V1.0
 */

public class SettingsActivity extends ActivitySupport {
	
	private static final String TAG = "SettingsActivity";

	private View mTitleBackView;
	private View mSelfSettingView;
	private Button mLogoutBtn;

	private AlertDialog alertDg = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.settings);

		init();
	}

	private void init() {
		icodeApplication.addActivity(this);

		mTitleBackView = findViewById(R.id.title_back_btn);
		mTitleBackView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		mSelfSettingView = findViewById(R.id.self_setting_txt);
		mSelfSettingView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(context, "我的信息", Toast.LENGTH_SHORT).show();
			}
		});

		mLogoutBtn = (Button) findViewById(R.id.logout_btn);
		mLogoutBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				alertDg = new AlertDialog.Builder(context)
						.setTitle(
								getResources().getString(
										R.string.settings_exit_btn))
						.setNeutralButton(
								getResources().getString(R.string.confirm),
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										new SecurityExitThread().start();
										dialog.cancel();
										pg.setMessage(getResources().getString(
												R.string.exiting_icode));
										pg.show();

									}
								})
						.setNegativeButton(
								getResources().getString(R.string.cancel),
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.cancel();
									}
								}).show();
			}
		});
	}

	private class SecurityExitThread extends Thread {

		@Override
		public void run() {
			AppConfigManager.getInstance().setUsername(null);
			AppConfigManager.getInstance().setPassword(null);
			AppConfigManager.getInstance().saveLoginConfig();
			icodeApplication.exit();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		LogUtil.i(TAG, "onKeyDown");
		if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){// back
			LogUtil.i(TAG, "点击了back或者home键");
			LogUtil.i(TAG, "pg is null " + (null == pg));
			if(null != pg){
				LogUtil.i(TAG, "关闭进度框");
				hideProgress();
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		LogUtil.i(TAG, "dispatchKeyEvent");
		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_HOME){// back
			LogUtil.i(TAG, "点击了back或者home键");
			LogUtil.i(TAG, "pg is null " + (null == pg));
			if(null != pg){
				LogUtil.i(TAG, "关闭进度框");
				hideProgress();
			}
		}
		return super.dispatchKeyEvent(event);
	}

}
